trigger InsertHCPChangeHistory on HCP__c (before update,after update) {
    if(trigger.new.size()>1 && system.trigger.isafter){
        list<id> hidl=new list<id>();
        list<id> rejectl=new list<id>();
        for(HCP__c h:trigger.new){
            if(Trigger.oldMap.get(h.id).approvalstatus__c == '审批通过但未生效' && h.approvalstatus__c=='审批通过'){
                hidl.add(h.id);
            }
            else if(Trigger.oldMap.get(h.Id).approvalstatus__c != h.approvalstatus__c && h.approvalstatus__c == '审批驳回'){
                rejectl.add(h.id);
            }
        }
        if(hidl.size()>0){
            InsertHCPChangeHistoryClass.ApproveMethod(hidl);
        }
        if(rejectl.size()>0){
            InsertHCPChangeHistoryClass.RejectMethod(rejectl);
        }
    }
    else if(trigger.new.size()==1 && system.trigger.isbefore){
        
        HCPChangeHistory__c HCH = null;
        String oldclass = null;
        String newclass = null;
        list<HCPChangeHistory__c> inshcp=new list<HCPChangeHistory__c>();
        for(HCP__c hcp : trigger.new){
            if(Trigger.oldMap.get(hcp.Id).approvalstatus__c != hcp.approvalstatus__c && 
               hcp.approvalstatus__c == '审批通过'){
                if(hcp.approvaltype__c == '新增医护人员'){
                    HCH = new HCPChangeHistory__c (HCP__c=hcp.Id, 
                                                   Name = hcp.approvaltype__c,
                                                   Old__c='',
                                                   New__c='',
                                                   comments__c=hcp.comments__c,
                                                   effectivedate__c=hcp.effectivedate__c);
                    hcp.Active__c = true;
                }
                else if(hcp.approvaltype__c == ''||hcp.approvaltype__c==null){
                    HCH = new HCPChangeHistory__c (HCP__c=hcp.Id, 
                                                   Name ='重新激活医护人员',
                                                   Old__c='',
                                                   New__c='',
                                                   comments__c=hcp.comments__c,
                                                   effectivedate__c=hcp.effectivedate__c);
                    hcp.Active__c = true;
                }
                else if(hcp.approvaltype__c == '删除医护人员'){
                    HCH = new HCPChangeHistory__c (HCP__c=hcp.Id, 
                                                   Name = hcp.approvaltype__c,
                                                   Old__c='',
                                                   New__c='',
                                                   comments__c=hcp.comments__c,
                                                   effectivedate__c=hcp.effectivedate__c);
                    hcp.Active__c = false;
                }
                else if(hcp.approvaltype__c == '改变医护人员级别'){
                    HCH = new HCPChangeHistory__c (HCP__c=hcp.Id, 
                                                   Name = hcp.approvaltype__c,
                                                   Old__c=hcp.class1__c + ', ' + hcp.class2__c + ', ' + hcp.class3__c,
                                                   New__c=hcp.changetoclass1__c + ', ' + hcp.changetoclass2__c + ', ' + hcp.classchangeto3__c,
                                                   comments__c=hcp.comments__c,
                                                   effectivedate__c=hcp.effectivedate__c);
                    hcp.class1__c = hcp.changetoclass1__c;
                    hcp.class2__c = hcp.changetoclass2__c;
                    hcp.class3__c = hcp.classchangeto3__c;
                }
    
                inshcp.add(HCH);
                
                
                hcp.approvaltype__c = null;
                hcp.classchangeto3__c = null;
                hcp.changetoclass1__c = null;
                hcp.changetoclass2__c = null;
                hcp.effectivedate__c = null;
                hcp.comments__c = null;
                hcp.approvalstatus__c =null;
            }
            if(Trigger.oldMap.get(hcp.Id).approvalstatus__c != hcp.approvalstatus__c && 
               hcp.approvalstatus__c == '审批驳回'){
                        
                        list<HCPChangeHistory__c> insertHistory=new list<HCPChangeHistory__c>();
                             HCH = new HCPChangeHistory__c (HCP__c=hcp.Id, 
                                                               Name = '审批拒绝:'+hcp.approvaltype__c,
                                                               Old__c='',
                                                               New__c='',
                                                               comments__c=hcp.comments__c,
                                                               effectivedate__c=date.today());
                            insertHistory.add(hch);
                            hcp.classchangeto3__c = null;
                            hcp.changetoclass1__c = null;
                            hcp.changetoclass2__c = null;
                            hcp.effectivedate__c = null;
                            hcp.comments__c = null;
                            hcp.approvalstatus__c=null;
                            hcp.approvaltype__c=null;
                        insert insertHistory;
            }
        }
        insert inshcp;
        
        
    }

}